<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<title>用户管理</title>
<body>
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'north',border:false" style="height: 40px;">
			<form id="schForm">
				<table>
					<tr>
						<td class="fm_lab">用户名:</td>
						<td><input name="usernameSch" type="text" class="easyui-validatebox"></td>
						<td><a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="crud.search();">查询</a></td>
					</tr>
				</table>
			</form>
		</div>

		<div id="toolbar">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="crud.add()">添加新用户</a>
		</div>

		<div data-options="region:'center',border:false">
			<table id="dg"></table>
		</div>
	</div>

	<div id="dlg" class="easyui-dialog" style="width: 450px; height: 150px;" closed="true" modal="true" buttons="#dlg-buttons">
		<form id="fm" method="post">
			<table class="table">
				<tbody>
					<tr>
						<th>用户名:</th>
						<td><input name="username" type="text" class="easyui-validatebox" >（请输入11位电话号码）</td>
					</tr>
					<tr>
						<th>密码:</th>
						<td><input id="password" name="password" type="password" class="easyui-validatebox" >（必填）</td>
					</tr>
				</tbody>
			</table>
		</form>
	</div>

	<div id="dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="crud.save(); return false;">保存</a> <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="crud.closeDlg(); return false;">取消</a>
	</div>

	<div id="addRoleDlg" class="easyui-dialog" style="width: 500px; height: 440px;" title="设置角色" data-options="onClose:Globle.clearPanel()" closed="true" modal="true">
		<div id="roleFrame"></div>
	</div>

	<script type="text/javascript">
		var that = this;
		var crud = Crud.create({
			pk : 'username',
			listUrl : ctx + 'listRUser.do',
			addUrl : ctx + 'addRUser.do',
			updateUrl : ctx + 'updateRUser.do',
			delUrl : ctx + 'delRUser.do',
			dlgId : 'dlg',
			formId : 'fm',
			gridId : 'dg',
			searchFormId : 'schForm',
			onBeforeSave : function(crud) {
				var $input = $('#password');
				var md5 = faultylabs.MD5($.trim($input.val()))
				$input.val(md5);
			}
		});

		var buttons = [ {
			text : '重置密码',
			onclick : function(row) {
				resetPassword(row);
			}
		}, {
			text : '设置角色',
			onclick : addRole
		} ];

		crud.buildGrid([ {
			field : 'username',
			title : '用户名',
			sortable : true
		}, {
			field : 'addTime',
			title : '添加时间',
			sortable : true
		}, {
			field : 'lastLoginDate',
			title : '最后登陆时间',
			sortable : true
		}, crud.createOperColumn(buttons) ]);

		function addRole(row) {
			var username = row.username;
			$('#roleFrame').html('<iframe src="userRole.jsp?username=' + username + '" scrolling="no" frameborder="0" style="width:100%;height:400px;"></iframe>');
			$('#addRoleDlg').dialog('open');
		}

		function closeDlg() {
			$('#addRoleDlg').dialog('close');
		}

		function resetPassword(row) {
			MsgUtil.confirm("确定给" + row.username + "重置密码吗?", function() {
				Action.jsonAsyncActByData(ctx + 'resetUserPassword.do', row, function(e) {
					if (e.success) {
						MsgUtil.alert('密码重置成功,新密码为:<strong style="color:red;">' + e.msg + '</strong>');
					}
				});
			});
		}
	</script>
</body>